﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Hi_Baidu.Net;
using Hi_Baidu.Packet;
using Hi_Baidu.ProcessModel;
using Hi_Baidu.Hooks;
using System.Windows.Input;
using Hi_Baidu.User.LogEntry;
using Hi_Baidu.Enum.User;

namespace Hi_Baidu
{
    /// <summary>
    ///     程序初始化类
    /// </summary>
    public class cls_Application_Init
    {      
        /// <summary>
        ///     程序初始化
        /// </summary>
        public static void Init()
        {
            Globle.user.DetectPort();
            if (Globle.user.LISTEN_PORT != -1 && Globle.user.TALK_PORT != -1 && Globle.user.VIDEO_PORT != -1 && Globle.user.VOICE_PORT != -1 && Globle.user.TRANSFER_FILE_PORT != -1)
            {
                CreatePort();
            }
            else
            {
                Win_Notify winNotify = new Win_Notify();
                winNotify.tb_Notify.Text = "当前程序数量已经到达上限!";
                winNotify.ShowDialog();
                Environment.Exit(0);
            }
            //寻找插件
            Globle.pluginManager.FindPlugin();
            //初始化网络信息事件池
            if (Globle.networkMessagePoolEvent == null)
            {
                Globle.networkMessagePoolEvent = new NetworkMessagePoolEvent();
            }
        }

        /// <summary>
        ///     检测当前程序相关文件
        /// </summary>
        public static void CheckReferenceFile(Window1 Win_Login)
        {
            if(!ClientProcessModel.CheckClientUserRoot())
            {
                ClientProcessModel.CreateClientUserRoot();
            }
            if (!ClientProcessModel.CheckClientUserFile())
            {
                ClientProcessModel.CreateClientUserFile();
            }
            ClientProcessModel.GetApplicationUser();
            List<IMetaData> ApplicationUserInfo = ClientProcessModel.ReadApplicationUserInfo();
            if (ApplicationUserInfo != null)
            {
                List<String> DIC_AppUserInfpo = new List<String>();
                foreach (TYPE_AppUserInfo item in ApplicationUserInfo)
                {
                    DIC_AppUserInfpo.Add(item.Account);
                }
                Win_Login.cmb_Username.DataContext = DIC_AppUserInfpo;
            }
        }
        
        /// <summary>
        ///     开启服务器相应端口
        /// </summary>
        private static void CreatePort()
        {
            SystemUdpPortListener Udp_Listener = null;
            //开启监听端口
            Udp_Listener = new SystemUdpPortListener(Globle.user.LISTEN_PORT, new SystemUdpMessageListener(3072), "NORMAL_UDP_LISTEN");
            if (!Globle.udpPortListenerManager.InstallListener(Udp_Listener))
            {
                throw new Exception("UDP 端口打开失败");
            }
            NetControl.processorRouter.InstallProcessor(new BasePacketListener());
            NetControl.processorRouter.InstallProcessor(new TalkMessagePacketListener());
            //开启视频端口
            Udp_Listener = new SystemUdpPortListener(Globle.user.VIDEO_PORT, new SystemUdpMessageListener(3072), "VIDEO_UDP_LISTEN");
            if (!Globle.udpPortListenerManager.InstallListener(Udp_Listener))
            {
                throw new Exception("UDP 端口打开失败");
            }
            //开启语音端口
            Udp_Listener = new SystemUdpPortListener(Globle.user.VOICE_PORT, new SystemUdpMessageListener(3072), "VOICE_UDP_LISTEN");
            if (!Globle.udpPortListenerManager.InstallListener(Udp_Listener))
            {
                throw new Exception("UDP 端口打开失败");
            }
            //开启文件传送端口
            Udp_Listener = new SystemUdpPortListener(Globle.user.TRANSFER_FILE_PORT, new SystemUdpMessageListener(3072), "TRANSFER_FILE_UDP_LISTEN");
            if (!Globle.udpPortListenerManager.InstallListener(Udp_Listener))
            {
                throw new Exception("UDP 端口打开失败");
            }
            INIT_NOTIFY_ICON();
        }

        /// <summary>
        ///    初始化任务栏图标
        /// </summary>
        public static void INIT_NOTIFY_ICON()
        {
            SetNoityIconByUserState(ONLIN_STATE.OFFLINE);
            Globle.user.NI.Visible = true;
            LoginProcessModel.TOOLTIP_NOTIFY_ICON();
        }

        /// <summary>
        ///     根据用户在线状态更改任务栏图标
        /// </summary>
        /// <param name="user_online_state">用户在线状态</param>
        public static void SetNoityIconByUserState(ONLIN_STATE user_online_state)
        {
            switch (user_online_state)
            {
                case ONLIN_STATE.ONLINE:
                    Globle.user.NI.Icon = Globle.user.ICON_ONLINE;
                    break;
                case ONLIN_STATE.HIDDEN:
                    Globle.user.NI.Icon = Globle.user.ICON_HIDDEN;
                    break;
                case ONLIN_STATE.BUSY:
                    Globle.user.NI.Icon = Globle.user.ICON_BUSY;
                    break;
                case ONLIN_STATE.AWAY:
                    Globle.user.NI.Icon = Globle.user.ICON_AWAY;
                    break;
                case ONLIN_STATE.OFFLINE:
                    Globle.user.NI.Icon = Globle.user.ICON_OFFLINE;
                    break;
            }
        }
    }
}
